//
//  YSStorageManager.m
//  YSBookRead
//
//  Created by Kyson on 2018/11/6.
//  Copyright © 2018 YangShen. All rights reserved.
//

#import "YSStorageManager.h"
#import "YSBookFileManager.h"
#import "YSReaderSetting.h"
#import "YSReadBookModel.h"
#import "YSBookChapterModel.h"
#import "YSReadRecordModel.h"
#import "YSBookSummaryModel.h"

static inline NSString *YSReaderSettingPath() {
    static NSString *settingPath;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        settingPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"reader_setting"];
    });
    return settingPath;
}

@implementation YSStorageManager

/// 阅读设置
+ (void)updateSetting:(YSReaderSetting *)setting {
    NSString *path = YSReaderSettingPath();
    [NSKeyedArchiver archiveRootObject:setting toFile:path];
}

+ (YSReaderSetting *)getSetting {
    NSString *path = YSReaderSettingPath();
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

/// 阅读的书籍模型
+ (void)saveReadBook:(YSReadBookModel *)readBook {
    [self keyedArchiver:readBook.bookID fileName:readBook.bookID object:readBook];
}

+ (YSReadBookModel *)getReadBook:(NSString *)bookID {
    return [self keyedUnarchiver:bookID fileName:bookID];
}

/// 章节列表
+ (void)saveBookChapterList:(NSArray<YSBookChapterModel *> *)bookChapterList bookID:(NSString *)bookID summaryID:(NSString *)summaryID {
    summaryID = summaryID ?: @"";
    [self keyedArchiver:bookID fileName:[[bookID stringByAppendingString:[NSString stringWithFormat:@"_%@",summaryID]] stringByAppendingString:@"_chapterList"] object:bookChapterList];
}

+ (NSArray<YSBookChapterModel *> *)getBookChapterList:(NSString *)bookID summaryID:(NSString *)summaryID {
    summaryID = summaryID ?: @"";
    return [self keyedUnarchiver:bookID fileName:[[bookID stringByAppendingString:[NSString stringWithFormat:@"_%@",summaryID]] stringByAppendingString:@"_chapterList"]];
}

/// 阅读历史记录
+ (void)saveReadRecord:(YSReadRecordModel *)readRecord {
    [self keyedArchiver:readRecord.bookID fileName:[readRecord.bookID stringByAppendingString:@"_record"] object:readRecord];
}

+ (YSReadRecordModel *)getReadRecord:(NSString *)bookID {
    return [self keyedUnarchiver:bookID fileName:[bookID stringByAppendingString:@"_record"]];
}

/// 单章节模型内容
+ (void)saveBookChapterBody:(NSString *)bookID chapterID:(NSString *)chapterID body:(NSString *)body {
    [self keyedArchiver:bookID fileName:chapterID object:body];
}

+ (NSString *)getBookChapterBody:(NSString *)bookID chapterID:(NSString *)chapterID {
    return [self keyedUnarchiver:bookID fileName:chapterID];
}

+ (void)saveBookSummary:(YSBookSummaryModel *)bookSummary bookID:(NSString *)bookID {
     [self keyedArchiver:bookID fileName:[bookID stringByAppendingString:@"_summary"] object:bookSummary];
}

+ (YSBookSummaryModel *)getBookSummary:(NSString *)bookID {
    return [self keyedUnarchiver:bookID fileName:[bookID stringByAppendingString:@"_summary"]];
}

+ (void)keyedArchiver:(NSString *)folderName fileName:(NSString *)fileName object:(id)object {
    NSString *path = [YSBookFileManager filePathWithFolderName:folderName fileName:fileName];
    [NSKeyedArchiver archiveRootObject:object toFile:path];
}

+ (id)keyedUnarchiver:(NSString *)folderName fileName:(NSString *)fileName {
    NSString *path = [YSBookFileManager filePathWithFolderName:folderName fileName:fileName];
    if (![YSBookFileManager fileIsExist:path]) {
        return nil;
    }
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

@end
